<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="com.xpy.utils.ConnSqlServer" %>
<%-- Created by IntelliJ IDEA.
User: 2314238429
Date: 2024/5/8
Time: 13:39
To change this template use File | Settings | File Templates.
--%>
<html>
<head>
    <title>新增日志</title>
    <link rel="stylesheet" href="css/style2.css">
    <style>
        .form-container {
            width: 50%;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 10px;
            box-shadow: 2px 2px 12px #aaa;
        }
        .form-container h2 {
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        .form-group label {
            width: 20%;
            text-align: right;
            margin-right: 10px;
        }
        .form-group input, .form-group textarea {
            width: 75%;
            padding: 10px;
            box-sizing: border-box;
        }
        .form-group textarea {
            height: 150px;
        }
        .form-buttons {
            text-align: center;
        }
        .form-buttons button {
            margin: 0 10px;
            padding: 10px 20px;
        }
    </style>
</head>
<body>

<div class="form-container">
    <h2>新增日志</h2>
    <form action="diary_new.jsp" method="post">
        <div class="form-group">
            <label for="title">*标题</label>
            <input type="text" id="title" name="title" required>
        </div>
        <div class="form-group">
            <label for="author">文章作者</label>
            <input type="text" id="author" name="author" required>
        </div>
        <div class="form-group">
            <label for="content">*内容</label>
            <textarea id="content" name="content" required></textarea>
        </div>
        <div class="form-buttons">
            <button type="submit">确定</button>
            <button type="reset">重置</button>
            <button type="button" onclick="window.location.href='diary_admin.jsp'">返回</button>
        </div>
    </form>
</div>

<%
    if ("POST".equalsIgnoreCase(request.getMethod())) {
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if (title != null && author != null && content != null) {
            Connection conn = null;
            PreparedStatement pstm = null;
            try {
                conn = ConnSqlServer.getConnection();
                pstm = conn.prepareStatement("INSERT INTO diary (title, author, content, wdate) VALUES (?, ?, ?, GETDATE())");
                pstm.setString(1, title);
                pstm.setString(2, author);
                pstm.setString(3, content);
                int result = pstm.executeUpdate();
                if (result > 0) {
                    out.println("<script>alert('日志添加成功！'); window.location.href='diary_admin.jsp';</script>");
                } else {
                    out.println("<script>alert('日志添加失败，请重试。');</script>");
                }
            } catch (Exception e) {
                e.printStackTrace();
                out.println("<script>alert('发生错误：" + e.getMessage() + "');</script>");
            } finally {
                if (pstm != null) try { pstm.close(); } catch (Exception e) { e.printStackTrace(); }
                if (conn != null) try { conn.close(); } catch (Exception e) { e.printStackTrace(); }
            }
        } else {
            out.println("<script>alert('所有字段都是必填的。');</script>");
        }
    }
%>

</body>
</html>
